﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 小小次朗点餐系统
{
    public partial class usergn2 : Form
    {
        //记录前一画面修改的行号
        public static int index = 0;
        //添加修改的识别变量 0:修改   1：菜单购买  3:购物车购买  4:修改个人信息 
        public static int type = 0;

        public static DataTable dt;
        public usergn2()
        {
            InitializeComponent();
        }
        private void usergn2_Load(object sender, EventArgs e) //画面控件加载更改和获取变量，行号
        {
            //添加画面切换
            if (type == 0)
            {
                //隐藏画面
                label2.Hide();
                textBox2.Hide();
                label3.Hide();
                textBox3.Hide();
                label4.Hide();
                textBox4.Hide();
                label5.Hide();
                textBox5.Hide();
                panel2.Hide();
                groupBox1.Hide();
                //设定画面名称
                this.Text = "修改商品数量";
                //设定按钮显示
                this.button1.Text = "修改";
            }
            if (type == 1)
            {
                //画面隐藏
                groupBox1.Hide();
                label3.Hide();
                textBox3.Hide();
                label4.Hide();
                textBox4.Hide();
                label5.Hide();
                textBox5.Hide();
                panel2.Show();
                //设定画面名称
                this.Text = "购买商品";
                //设定按钮显示
                this.button1.Text = "购买";

                //获取当前登录账户的id
                string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
                string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

                string sql = string.Format($"select Address from [User] where UserID='{UserID}'");
                string address = DBHelper.ExecuteScalar(sql).ToString();
                string sql1 = string.Format($"select Phone from [User] where UserID='{UserID}'");
                string phone = DBHelper.ExecuteScalar(sql1).ToString();
                string sql2 = string.Format($"select Name from [User] where UserID='{UserID}'");
                string name = DBHelper.ExecuteScalar(sql2).ToString();
                //画面账号控件设定
                this.richTextBox1.Text = address;
                //画面用户名控件设定
                this.textBox6.Text = phone;
                //画面用户类型控件设定
                this.textBox8.Text = name;
            }
            if (type == 3)
            {
                //画面隐藏
                groupBox1.Hide();
                textBox1.Hide();
                label1.Hide();
                label3.Hide();
                textBox3.Hide();
                label4.Hide();
                textBox4.Hide();
                label5.Hide();
                textBox5.Hide();
                panel2.Show();
                //设定画面名称
                this.Text = "购买商品";
                //设定按钮显示
                this.button1.Text = "购买";

                //获取当前登录账户的id
                string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
                string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

                string sql = string.Format($"select Address from [User] where UserID='{UserID}'");
                string address = DBHelper.ExecuteScalar(sql).ToString();
                string sql1 = string.Format($"select Phone from [User] where UserID='{UserID}'");
                string phone = DBHelper.ExecuteScalar(sql1).ToString();
                string sql2 = string.Format($"select Name from [User] where UserID='{UserID}'");
                string name = DBHelper.ExecuteScalar(sql2).ToString();
                //画面账号控件设定
                this.richTextBox1.Text = address;
                //画面用户名控件设定
                this.textBox6.Text = phone;
                //画面用户类型控件设定
                this.textBox8.Text = name;
            }
            if (type == 4)
            {
                groupBox1.Hide();
                panel2.Hide();
                //设定画面名称
                label1.Text = "地址：";
                label2.Text = "性别：";
                label3.Text = "电话：";
                label4.Text = "密码：";
                label5.Text = "姓名：";
                this.Text = "修改个人信息";
                //设定按钮显示
                this.button1.Text = "确认修改信息";

                //需要把修改的数据传递过来
                DataRow dr = usergn1.dt5.Rows[index];
                //画面账号控件设定
                this.textBox1.Text = dr["Address"].ToString();
                //画面密码控件设定
                this.textBox2.Text = dr["Sex"].ToString();
                //画面用户名控件设定
                this.textBox3.Text = dr["Phone"].ToString();
                //画面用户类型控件设定
                this.textBox4.Text = dr["Pwd"].ToString();
                //画面用户类型控件设定
                this.textBox5.Text = dr["Name"].ToString();
            }
            if (type == 5)
            {
                //隐藏画面
                label2.Hide();
                textBox2.Hide();
                label3.Hide();
                textBox3.Hide();
                label4.Hide();
                textBox4.Hide();
                label5.Hide();
                textBox5.Hide();
                panel2.Hide();
                groupBox1.Show();

                //获取当前登录账户的id
                string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
                string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();
                //查询消费记录
                string sql = string.Format($"select * from [Order] where UserID='{UserID}' order by OrderTime desc");
                DataSet ds = DBHelper.SelectByAdapter(sql);

                dt = ds.Tables[0];
                this.dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.DataSource = dt;
                
                string sqlUser = string.Format($"select Money from [User] where UserID='{UserID}'");
                string Money = DBHelper.ExecuteScalar(sqlUser).ToString();
                label12.Text = Money;
                //设定画面名称
                this.Text = "充值系统";
                label1.Text = "充值金额";
                //设定按钮显示
                this.button1.Text = "确认充值";
            }
        }
        private void button1_Click(object sender, EventArgs e) //判断功能 进行修改或购买
        {
            if (type == 0)
            {
                try
                {
                    int Count = int.Parse(textBox1.Text);
                    if (textBox1.Text == "" || Count == 0)
                    {
                        MessageBox.Show("不能输入0");
                    }
                    else
                    {
                        //********************数据库数据更新
                        //取得所选行的用户编号----作为更改的定位条件
                        string sid = usergn1.dt3.Rows[index]["ShoppingID"].ToString();
                        string fid = usergn1.dt3.Rows[index]["FoodID"].ToString();

                        string sql1 = string.Format($"select FoodPrice from Food where FoodID='{fid}'");
                        string ShoppingPrice = DBHelper.ExecuteScalar(sql1).ToString();
                        decimal Price = decimal.Parse(ShoppingPrice);

                        string sql = string.Format($"update Shopping set ShoppingCount='{textBox1.Text}' where ShoppingID='{sid}'");
                        bool rlt = DBHelper.GetNonQuery(sql);
                        if (rlt)
                        {
                            MessageBox.Show("修改成功");
                            //********************画面控件数据更新
                            //取得画面选中行
                            DataRow dr = usergn1.dt3.Rows[index];
                            //开始编辑
                            dr.BeginEdit();
                            dr["ShoppingPrice"] = Count * Price;
                            dr["ShoppingCount"] = textBox1.Text;
                            dr.EndEdit();
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("输入错误或网络繁忙！", "提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            if (type == 1)
            {
                try
                {
                    int Count = int.Parse(textBox1.Text);
                    string Note = textBox2.Text;
                    if (Count == 0 || textBox6.Text == "" || textBox8.Text == "" || richTextBox1.Text == "")
                    {
                        MessageBox.Show("不能输入空白或0");
                    }
                    else
                    {
                        string State = "等待接单";
                        string FoodID = usergn1.dt2.Rows[index]["FoodID"].ToString();//定位

                        string sql1 = string.Format($"select FoodPrice from Food where FoodID='{FoodID}'");
                        decimal FoodPrice = decimal.Parse(DBHelper.ExecuteScalar(sql1).ToString());

                        string sqltime = "select GETDATE()";
                        string datetime = DBHelper.ExecuteScalar(sqltime).ToString();

                        string sql = string.Format($"select MeID from Food where FoodID='{FoodID}'");
                        string MeID = DBHelper.ExecuteScalar(sql).ToString();
                        //获取当前登录账户的id
                        string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
                        string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

                        decimal ShoppingPrice = Count * FoodPrice;
                        //扣钱
                        //decimal xMoney=0;

                        string sqlMoney = string.Format($"select Money from [User] where UserID='{UserID}'");
                        decimal Money = decimal.Parse(DBHelper.ExecuteScalar(sqlMoney).ToString());
                        if (Money > ShoppingPrice)
                        {
                            decimal xMoney = Money - ShoppingPrice;
                            string sqlxMoney = string.Format($"update [User] set Money='{xMoney}' where UserID='{UserID}'");
                            bool rlt = DBHelper.GetNonQuery(sqlxMoney);
                            if (rlt)
                            {
                                //MessageBox.Show("修改成功");
                                //订单表
                                string sql4 = string.Format($"insert into [Order] values('{Note}','{ShoppingPrice}','{UserID}','{State}','{datetime}','{Count}','{richTextBox1.Text}','{textBox6.Text}','{textBox8.Text}')");
                                bool result = DBHelper.GetNonQuery(sql4);
                                //详单表
                                string sqlOrderID = string.Format($"select  top 1 * from [Order]  order by OrderID desc");
                                decimal OrderID = decimal.Parse(DBHelper.ExecuteScalar(sqlOrderID).ToString());
                                string sql4Detailed = string.Format($"insert into Detailed values('{FoodPrice}','{Count}','{OrderID}','{FoodID}','{MeID}')");
                                bool rsDetailed = DBHelper.GetNonQuery(sql4Detailed);
                                if (result && rsDetailed)
                                {
                                    MessageBox.Show("购买成功，祝您购物愉快！");
                                }
                            }

                        }
                        else
                        {
                            MessageBox.Show("余额不足，请及时充值！");
                        }


                    }
                }
                catch
                {
                    MessageBox.Show("输入错误或网络繁忙！", "提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            if (type == 3)
            {
                try
                {
                    string State = "等待接单";

                    string Note = textBox2.Text;
                    int Count = int.Parse(usergn1.dt3.Rows[index]["ShoppingCount"].ToString());

                    string FoodID = usergn1.dt3.Rows[index]["FoodID"].ToString();

                    string sql1 = string.Format($"select FoodPrice from Food where FoodID='{FoodID}'");
                    decimal FoodPrice = decimal.Parse(DBHelper.ExecuteScalar(sql1).ToString());

                    string sqltime = "select GETDATE()";
                    string datetime = DBHelper.ExecuteScalar(sqltime).ToString();

                    string sql = string.Format($"select MeID from Food where FoodID='{FoodID}'");
                    string MeID = DBHelper.ExecuteScalar(sql).ToString();
                    //获取当前登录账户的id
                    string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
                    string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

                    decimal ShoppingPrice = Count * FoodPrice;

                    string sqlMoney = string.Format($"select Money from [User] where UserID='{UserID}'");
                    decimal Money = decimal.Parse(DBHelper.ExecuteScalar(sqlMoney).ToString());
                    if (Money > ShoppingPrice)
                    {
                        decimal xMoney = Money - ShoppingPrice;
                        string sqlxMoney = string.Format($"update [User] set Money='{xMoney}' where UserID='{UserID}'");
                        bool rlt = DBHelper.GetNonQuery(sqlxMoney);
                        if (rlt)
                        {
                            MessageBox.Show("购买成功，祝您购物愉快！");
                            //订单表
                            string sql4 = string.Format($"insert into [Order] values('{Note}','{ShoppingPrice}','{UserID}','{State}','{datetime}','{Count}','{richTextBox1.Text}','{textBox6.Text}','{textBox8.Text}')");
                            bool result = DBHelper.GetNonQuery(sql4);
                            //详单表
                            string sqlOrderID = string.Format($"select  top 1 * from [Order]  order by OrderID desc");
                            decimal OrderID = decimal.Parse(DBHelper.ExecuteScalar(sqlOrderID).ToString());
                            string sql4Detailed = string.Format($"insert into Detailed values('{FoodPrice}','{Count}','{OrderID}','{FoodID}','{MeID}')");
                            bool rsDetailed = DBHelper.GetNonQuery(sql4Detailed);
                        }

                    }
                    else
                    {
                        MessageBox.Show("余额不足，请及时充值！");
                    }
                }
                catch
                {
                    MessageBox.Show("输入错误或网络繁忙！", "提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
            if (type == 4)
            {
                try
                {
                    //********************数据库数据更新
                    //取得所选行的用户编号----作为更改的定位条件
                    if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
                    {
                        MessageBox.Show("不能留空！！");
                    }
                    else
                    {
                        string id = usergn1.dt5.Rows[index]["UserID"].ToString();
                        string sql = string.Format($"update [User] set Name='{textBox5.Text}',Pwd='{textBox4.Text}',Phone='{textBox3.Text}',Sex='{textBox2.Text}',Address='{textBox1.Text}' where UserID={id}");
                        bool rlt = DBHelper.GetNonQuery(sql);
                        if (rlt)
                        {
                            MessageBox.Show("修改成功,请重新刷新");
                            //********************画面控件数据更新
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("输入错误或网络繁忙！", "提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }


        private void button2_Click(object sender, EventArgs e)//充值判断
        {
            try
            {
                //获取当前登录账户的id
                string sqlsuerID = string.Format($"select UserID from [User] where Unumber='{userLogin.suserName}' and Pwd='{userLogin.spassWord}'");
                string UserID = DBHelper.ExecuteScalar(sqlsuerID).ToString();

                decimal cz = int.Parse(textBox7.Text);
                DialogResult rlt = MessageBox.Show("确认充值吗？", "提示：", MessageBoxButtons.YesNo);
                if (rlt == DialogResult.Yes)
                {
                    string sqlMoney = string.Format($"select Money from [User] where UserID='{UserID}'");
                    decimal Money = decimal.Parse(DBHelper.ExecuteScalar(sqlMoney).ToString());
                    decimal xMoney = Money + cz;
                    string sqlxMoney = string.Format($"update [User] set Money='{xMoney}' where UserID='{UserID}'");
                    bool rlt1 = DBHelper.GetNonQuery(sqlxMoney);
                    if (rlt1)
                    {
                        MessageBox.Show("充值成功！");
                        label12.Text = xMoney.ToString();
                    }
                }

            }
            catch
            {
                MessageBox.Show("输入错误或网络繁忙！", "提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        #region 限制输入字母还是数字
        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                e.Handled = !"01234567890".Contains(e.KeyChar);
            }
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                e.Handled = !"01234567890".Contains(e.KeyChar);
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void textBox4_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                e.Handled = !"01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".Contains(e.KeyChar);
            }
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\b')//这是允许输入退格键 
            {
                e.Handled = !"01234567890".Contains(e.KeyChar);
            }
        }

        #endregion

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
